package com.tngames.angrytunisian.game.core;

import static playn.core.PlayN.assets;
import static playn.core.PlayN.graphics;

import java.awt.event.MouseAdapter;

import org.jbox2d.common.Vec2;

import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Mouse;
import playn.core.Mouse.ButtonEvent;
import playn.core.Mouse.MotionEvent;
import playn.core.Mouse.WheelEvent;

import com.tngames.angrytunisian.game.core.entity.EntityEngine;
import com.tngames.angrytunisian.game.core.camera.Camera;
import com.tngames.angrytunisian.game.core.splingshot.PeaTest;
import com.tngames.angrytunisian.game.core.splingshot.SlingShot;

public class AngryTunisianGameEngine implements GameScreen {

	private AngryTunisianGame game;

	private static SlingShot slingShot;
	private static PeaTest pea;

	private EntityEngine entityEngine;
	
	private final Camera camera;

	public AngryTunisianGameEngine(AngryTunisianGame game) {
		this.game = game;

		graphics().setSize(Constants.SCREEN_PIXEL_WIDTH,
				Constants.SCREEN_PIXEL_HEIGHT);
		GroupLayer worldlayer = buildWorldLayer();
		Vec2 gravity = new Vec2(0.0f, 10.0f);

		entityEngine = new EntityEngine(worldlayer, gravity, 640, 480,
				Constants.PHYS_UNIT_PER_SCREEN_UNIT, Constants.DEBUG_DRAW);

		camera = new Camera(entityEngine);
		
//		slingShot = new SlingShot(entityEngine,
//				200 * Constants.PHYS_UNIT_PER_SCREEN_UNIT,
//				200 * Constants.PHYS_UNIT_PER_SCREEN_UNIT);
//		pea = new PeaTest(entityEngine, slingShot.getPosX(),
//				slingShot.getPosX());
		pea = new PeaTest(entityEngine, 20*Constants.PHYS_UNIT_PER_SCREEN_UNIT,
				20*Constants.PHYS_UNIT_PER_SCREEN_UNIT);

		// PeaTest pea = new PeaTest(entityEngine,
		// 40 * Constants.PHYS_UNIT_PER_SCREEN_UNIT,
		// 40 * Constants.PHYS_UNIT_PER_SCREEN_UNIT);

		entityEngine.add(pea);
		//entityEngine.add(slingShot);
		
		playn.core.PlayN.mouse().setListener(new Mouse.Adapter(){
			@Override
			public void onMouseWheelScroll(WheelEvent event) {
				super.onMouseWheelScroll(event);
				camera.scroll(event);
			}
		});
	}

	@Override
	public void update(float delta) {
		camera.update(delta);
	}

	// create our world layer (scaled to "world space")
	// main layer that holds the world. note: this gets scaled to world space
	private GroupLayer buildWorldLayer() {
		Image backgroundImage = assets().getImage(Resources.BACKGROUND);
		ImageLayer backgroundLayer = graphics().createImageLayer(
				backgroundImage);
		backgroundLayer.setTranslation(0, 0);
		graphics().rootLayer().add(backgroundLayer);

		GroupLayer worldLayer = graphics().createGroupLayer();
		// worldLayer.setTranslation(Constants.BOARD_OFFSET_X,
		// Constants.BOARD_OFFSET_Y);
		worldLayer.setScale(1f / Constants.PHYS_UNIT_PER_SCREEN_UNIT);
		graphics().rootLayer().add(worldLayer);
		return worldLayer;
	}

	public static SlingShot getSlingShot() {
		return slingShot;
	}
	
	public static PeaTest getPea(){
		return pea;
	}

}
